package com.zzl.myblog.View.Activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.zzl.myblog.Bean.ResponseBean.UserBean;
import com.zzl.myblog.R;
import com.zzl.myblog.Utils.HttpUtil;
import com.zzl.myblog.Utils.responseUtility;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class RegisterActivity extends AppCompatActivity {
    static final String TAG = "RegisterActivity";
    Context context;
    EditText username_et,password_et,password_et2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        context = getBaseContext();
        iniUi();
    }

    private void iniUi(){
        username_et = findViewById(R.id.et_user_number_register);
        password_et = findViewById(R.id.et_password_register);
        password_et2 = findViewById(R.id.et_password_2_register);
    }

    /**
     * 用户注册
     */
    public void register(View view){
        String username = username_et.getText().toString().trim();
        String password1 = password_et.getText().toString().trim();
        String password2 = password_et2.getText().toString().trim();

        // 判断两次密码是否一致
        if(password1.equals(password2)){
            Map<String, String> map = new HashMap<>();
            map.put("username", username);
            map.put("password", password1);
            String url = "users";
            HttpUtil.user_register_POST(url,map, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Looper.prepare();
                            sendToast("请求失败");
                            Log.i(TAG,e.getMessage());
                            Looper.loop();
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            //拿到JSON格式的数据
                            final String responseJson=response.body().string();
                            final  UserBean  userBean= responseUtility.parse_user(responseJson);
                            Looper.prepare();
                            if(userBean.getStatus().equals("201")){
                                sendToast("注册成功!");
                            }else{
                                sendToast("注册失败!");
                            }
                            Looper.loop();
                        }
                    });
        }else{
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    sendToast("两次密码不一致，请重新输入！");
                    // 重置输入
                    password_et.setText("");
                    password_et2.setText("");
                }
            });

        }
    }

    /**
     * back[跳到登录page]
     */
    public void back(View view ){
        onBackPressed();
    }

    private void sendToast(String msg){
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
}
